 #include"SceneNode.h"

SceneNode::SceneNode(Mesh* mesh , Vector4 colour) {
	this->mesh = mesh;
	this->shader = NULL;
	this->colour = colour;
	parent = NULL;
	Type = 0;
	//texture = -1;
	modelScale = Vector3(1,1,1);

	boundingRadius = 1.0f;
	distanceFromCamera = 0.0f;

	aspectRatio = 1.0f;
	draw = true;
}

SceneNode::~SceneNode() {
	for(unsigned int i = 0; i < children.size(); ++i) {
		delete children[i];
	}
}

void SceneNode::AddChild(SceneNode* s) {
	////cout << "Child Added: " << s << endl;
	children.push_back(s);
	s->parent = this;
}

void SceneNode::Draw(OGLRenderer& r) {
	if(mesh && draw) { mesh->Draw();}
}

void SceneNode::Update(float msec) {
	if(parent) {//This node has a parent...
		worldTransform = parent ->worldTransform * transform;
	}else{//Root node, world transform is local transform!
		worldTransform = transform;
	}

	for(vector <SceneNode*>::iterator i = children.begin(); i != children.end(); ++i) {
		(*i)->Update(msec);
	}
}

void SceneNode::RemoveChild(SceneNode* s){
	for(vector <SceneNode*>::iterator i = children.begin(); i != children.end(); ++i) {
		if(*i==s){
			//Remove i
			children.erase(i);
			return;
		}
	}
}